package com.laicunba.service.accounting.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.pojo.FinancingTargetDO;
import com.laicunba.pojo.UserDO; 
import com.laicunba.service.FinancingTargetBidService;
import com.laicunba.service.FinancingTargetService;
import com.laicunba.service.SaleService;
import com.laicunba.service.UserService;
import com.laicunba.service.WithdrawService;
import com.laicunba.service.accounting.AccountingTemplate;
import com.laicunba.service.accounting.DemandBid2FixService; 
import com.laicunba.util.AssertUtil;
import com.laicunba.util.SeqUtil;
import com.laicunba.util.StringUtil;

@Service
public class DemandBid2FixServiceImpl extends AccountingTemplate implements DemandBid2FixService {

	@Autowired
	private UserService userService;
	@Autowired
	private FinancingTargetBidService financingTargetBidService;
	@Autowired
	private FinancingTargetService financingTargetService;
	@Autowired
	private SaleService saleService;
	@Autowired
	private WithdrawService withdrawService; 
	
	@Transactional
	@Override
	public String doTransfor(String userId, String financingTargetId, BigDecimal amount,boolean transferCurrent) { 
		return doTransfor(userId,financingTargetId,amount,null,transferCurrent,null);
	}
	
	@Transactional
	@Override
	public String doTransfor(String userId, String financingTargetId, BigDecimal amount,String cashCouponId,boolean transferCurrent,String rateIncrCouponId) {
		UserDO user = userService.get(UserDO.class, userId);
		AssertUtil.assertNotNull(user, "USER_NOT_EXIST");
		FinancingTargetDO financingTargetDO = financingTargetService.get(FinancingTargetDO.class, financingTargetId);
		AssertUtil.assertNotNull(financingTargetDO, "FINANCING_TARGET_NOT_EXIST");
		AssertUtil.assertTrue("CPB".equalsIgnoreCase(financingTargetDO.getType()));
		AssertUtil.assertTrue("onsale".equalsIgnoreCase(financingTargetDO.getStatus()));
		if (financingTargetDO.getFinishAmount() == null) {
			financingTargetDO.setFinishAmount(BigDecimal.ZERO);
		}
		AssertUtil.assertTrue(financingTargetDO.getCollectAmount().subtract(financingTargetDO.getFinishAmount()).compareTo(amount) >= 0, "NO_ENOUGH_AMOUNT");
		// 随存宝自动
		WithdrawService.SCBWithdrawParam param = new WithdrawService.SCBWithdrawParam();
		param.setTry(false);
		param.setUserId(userId);
		param.setWithdrawAmount(amount);
		param.setMark("DEMAND_2_FIX");
		WithdrawService.SCBWithdrawResult result = withdrawService.doWithDrawApply(param);

		AssertUtil.assertNotNull(result, "WITHDRAW_APPLY_ERROR");

		WithdrawService.DoWithdrawParam confirmWithdrawParam = new WithdrawService.DoWithdrawParam();
		confirmWithdrawParam.setWidthdrawId(result.getWithdrawId());
		AssertUtil.assertTrue(withdrawService.doWithDraw(confirmWithdrawParam), "WITHDRAW_ERROR");

		// 自动购买存票宝
		String certificateId = "D2F-" + result.getWithdrawId();
		SaleService.MakeFinancingTargetBidParam lockParam = new SaleService.MakeFinancingTargetBidParam();
		lockParam.setCertificateId(certificateId);
		lockParam.setFinancingTargetId(financingTargetDO.getId());
		lockParam.setBuyAmount(amount);
		lockParam.setBuyUserId(userId);
		lockParam.setCashCouponId(cashCouponId);
		lockParam.setTransferCurrent(transferCurrent);
		lockParam.setRateIncrCouponId(rateIncrCouponId);
		lockParam.setSeq(SeqUtil.genNewBidSeq());
		AssertUtil.assertTrue(saleService.makeFinancingTargetBid(lockParam), "LOCK_ERROR");

		SaleService.PayedFinancingTargetParam payedParam = new SaleService.PayedFinancingTargetParam();
		payedParam.setCertificateId(certificateId);
		payedParam.setPayMark("DEMAND_2_FIX");
		// payedParam.settN(0);
		AssertUtil.assertTrue(saleService.payedFinancingTarget(payedParam), "PAYED_ERROR");

		return certificateId;
	}

}
